<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Models\Goods;
use App\Models\order_goods;
use App\Models\orders;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class OrderController extends Controller
{	
	/**
	 * 下单操作	
	 */
	public function insert(Request $request)
	{	 
        if(empty(session('id'))){
           echo '<script>alert("请先登录用户");window.location="/log/log/log";</script>';
        }
		//验证表单
    	$this->validate($request, [
	        'phone' => 'required|regex:[^\d{11}]',
	        'name' => 'required',
	        'address' => 'required',
    	],[
    		'phone.required'=>'联系电话不能为空',
    		'phone.regex'=>'请输入正确的号码',
    		'name.required'=>'收货人不能为空',
    		'address.required'=>'收货地址不能为空',
    	]);
    	//用户
    	
    	// dd(session()->all());   // request ->    "id" => 42   "member" => "adminsss"
    	// dd($request->all());"name" => "我的"  "address" => "北京市"  "phone" => "15544332211" "orderprice" => "738"

    	//orders表数据
    	$orders=new orders;
    	//创建数据
    	$orders->userid = session('id');
    	$orders->order_code = date('Ymd').time().rand(11111,222222);
        // dd($orders->order_code);
    	$orders->orderprice = $request->input('orderprice');
    	$orders->name = $request->input('name');
    	$orders->address = $request->input('address');
    	$orders->phone = $request->input('phone');

    	// 执行插入
    	if(!$orders->save()){
    		echo '提交订单失败<a href="/cart/index">点击返回</a>';
    		die;
    	}


     /**
         *创建订单
         * order_goods表操作
         */
        $session = session('cart');
    /*
        	//获取购物车商品 计算总价
	        $t=0;
	        foreach ($session as $k => $v) {
	            $price=Goods::find($v['id'])->price;
	            $t += $v['num']*$price;
	        }
	        $data=[];
    */
        // 获取order_goods字段
        foreach ($session as $k => $v) {
            $tmp['orderid']=$orders->id;
            $tmp['goodid']=$v['id'];
            $tmp['number']=$v['num'];
            $tmp['unit_price']=Goods::find($v['id'])->price;
            $data[]=$tmp;
        }
        // 最后插入数据的ID
        $OrderId=$tmp['orderid'];
        // dd($orders);
        $res=DB::table('order_goods')->insert($data);
        if($res){
            session('zhifu');
            session(['zhifu' => session('cart')]);
            //重定向到支付页面   分配数据  显示订单ID..........
            return redirect()->action('OrderController@zhifu', ['OrderId'=>$OrderId]);
        }
        

    }



    //支付订单页
    public function zhifu(Request $request)
    {   
        
        //清空购物车
        $request->session()->forget('cart');
        //获取OrderController控制器insert方法传过来的参数   返回订单表插入的最后一条数据orderid
        $id = $request->input('OrderId');
        //根据订单表orderid查询订单
        $res = DB::table('orders') -> where('orderid',$id)->first();
        //查询商品信息
        $ress=DB::table('order_goods')->where('orderid',$id)->get();
        // 根据order_goods表中goodid遍历查询出商品
        foreach ($ress as $k => $v) {
            $a[]=$v->goodid;
            
            
        }
        //订单中的商品
        $resss=Goods::find($a);

        return view('cart/zhifu',['data'=>$res,'dingdan'=>$resss,'id'=>$id]);
    }

    public function zhifule(Request $request)
    {   
        //获取订单表ID
        $orderid = $request->input('orderid');
        //修改订单状态为1   已付款
        $dingdan = DB::table('orders')->where('orderid', $orderid)->update(['stat' => 1]);
        //先登录用户
        if(empty(session('id')))
        {
            echo '<script>alert("请先登录用户");window.location="/log/log/log";</script>';
        }
        //用户ID
        $id=session('id');
        //公司账户 + 钱      用户传过来的总价
        $money=$request->input('orderprice');
        //查询当前用户的余额
        $qian = DB::table('member')->where('id',$id)->first()->qian;
        //交易操作
        $res=DB::update('update member set qian = '.($qian-$money).' where id=?',[$id]);
        //获取当前公司账户的金额
        $res = DB::table('money')->first()->money;
        if($res){
            $r = DB::update('update money set money = '.($res+$money).' where id=?',[1]);
            // $c = DB::update('update ')

            echo '<script>alert("付款成功");window.location="/";</script>';
        }else{
            echo '交易失败, <a href="/">点击返回首页</a>';
        }
    }
}
